Start Visual Basic 6.0 and create a new Standard EXE Application.
Select Project -> References... from the menu to open up the References dialogue.
Scroll down until you locate the mobileFX WebSocketX and select it. Then click OK.
In order to test WebSocketX, we will need an Echo Server. For the purpose of this tutorial we use mobileFX's echo server. Creation of the Echo Server is out of scope of this tutorial, but you can easily find instructions on how to build your own or use an online echo server, instead.
Create a simple form, with the following controls:
Control Name | Control Type | Text | Click Handler | Description |
---|---|---|---|---|
btnConnect | Button | Connect | btnConnect_Click | Connect to Echo Server |
btnSendMessage | Button | Send Message | btnSendMessage_Click | Send a custom message to the Web Socket echo server |
btnDisconnect | Button | Disconnect | btnDisconnect_Click | Disconnect |
txtMessage | Text | Allows user to enter a message to send to the Web Socket echo server | ||
txtOutput | Text | (Readonly) Responses and messages that we want to show to the user are set in this control. |
The form looks like this:
Copy and paste the code below, to test WebSocketX:
VB6 |
Copy Code |
---|---|
Option Explicit Dim WithEvents ws As WebSocketX.WebSocket Private Sub Form_Load() btnDisconnect.Enabled = False ' VB controls need Charset to properly work with Unicode Me.Font.Charset = 161 Set txtMessage.Font = Me.Font Set txtOutput.Font = Me.Font ' Create WebSocketX instance Set ws = New WebSocketX.WebSocket ' Set WebSocket text mode and IO buffers ws.TextMode = True ws.MaximumIncomingMessageSizeBytes = 1000000# ws.WriteBufferSizeBytes = 1000000# End Sub Private Sub btnConnect_Click() txtOutput.Text = "Connecting ..." btnDisconnect.Enabled = True ' Connect to WebSocket Echo Server in insecure mode ws.Open txtServerURL.Text, WEBSOCKET_SECURITY_ENUM.INSEURE End Sub Private Sub btnDisconnect_Click() txtOutput.Text = "Disconnecting ..." btnConnect.Enabled = True btnDisconnect.Enabled = False ' Disconnect from server and transmit closing frame ws.Close CLOSE_CODE_NORMAL End Sub Private Sub ws_OnOpen() txtOutput.Text = "Connected." End Sub Private Sub ws_OnError(ByVal ErrorCode As Long, ByVal ErrorDescription As String) txtOutput.Text = "ERROR: " & ErrorCode & ", " & ErrorDescription End Sub Private Sub ws_OnClose() txtOutput.Text = "Disconnected." End Sub Private Sub ws_OnMessage(ByVal data As String) txtOutput.Text = txtOutput.Text + vbCrLf + data End Sub Private Sub btnSendMessage_Click() If IsEmpty(txtMessage.Text) Then MsgBox "Please enter message", vbCritical Exit Sub End If If ws.State <> WS_OPEN Then MsgBox "Please connect to WebSocket echo server", vbCritical Exit Sub End If ws.Send txtMessage.Text End Sub |
That's it. Build and run the project. You should see something like this: